﻿<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Laharsub Manual Tests</title>

    <script src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script src="jquery.pubsub.js"></script>

    <script language="javascript">

        function addNotification(text) {
            var notifications = document.getElementById("notifications");
            notifications.value += text + "\n";
            notifications.scrollTop = notifications.scrollHeight;
        }

        function onSubscribe() {
            var sub = {
                topicId : document.getElementById("topicId").value - 0,
                onMessageReceived : function(message) {
                    addNotification(message.topicId + "/" + message.messageId + ":\n" + message.body);
                },
                onError : function(args) {
                    addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                }
            }

            if (document.getElementById("messageId").value.length > 0)
                sub.from = document.getElementById("messageId").value - 0;

            $.pubsub.subscribe(sub);
            
            addNotification("Subscribed to topic " + document.getElementById("topicId").value);
        }

        function onPublish() {
            $.pubsub.publish({
                topicId : document.getElementById("topicId").value - 0,
                contentType : "text/plain",
                body : document.getElementById("message").value,
                onSuccess : function(args) {
                    addNotification("Message published: " + args.messageId);
                    document.getElementById("messageId").value = args.messageId;
                },
                onError : function(args) {
                    addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                }
            })
        }

        function onCreateTopic() {
            $.pubsub.createTopic({
                onSuccess : function(args) {
                    addNotification("Topic created: " + args.topicId);
                    document.getElementById("topicId").value = args.topicId;
                },
                onError : function(args) {
                    addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                }
            })
        }

        function onUnsubscribe() {
            $.pubsub.unsubscribe(document.getElementById("topicId").value - 0);
            addNotification("Unsubscribed from topic " + document.getElementById("topicId").value);
        }

        function onUnsubscribeAll() {
            $.pubsub.unsubscribeAll();
            addNotification("Unsubscribed from all topics");
        }

        var _latencyTestSent;
        var _latencyTestInterval;
        var _latencyTestTopicId;
        var _latencyTestError;
        function _latencyTestPublishOne() {
            if (_latencyTestSent > 0 && !_latencyTestError) {
                _latencyTestSent--;
                $.pubsub.publish({
                    topicId : _latencyTestTopicId,
                    contentType : "text/plain",
                    body : new Date().getTime().toString(),
                    onError : function(args) {
                        addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                        _latencyTestError = true;
                        addNotification("----- end latencyTest -----");
                    }
                });
            }
            else {
                window.clearInterval(_latencyTestInterval);
            }
        }

        // TODO: there seems to be a race condition in the latency test at high publishing frequencies (10ms)

        function latencyTest() {
            addNotification("----- start latencyTest -----");
            $.pubsub.createTopic({
                onSuccess : function(args) {
                    addNotification("Topic created: " + args.topicId);
                    document.getElementById("topicId").value = args.topicId;

                    var messagesToSend = document.getElementById("messageNum").value - 0;
                    var messageDelay = document.getElementById("messageDelay").value - 0;
                    _latencyTestError = false;
                    var received = 0;
                    var cummulativeTime = 0;
                    var max = -1;
                    var min = -1;

                    $.pubsub.subscribe({
                        topicId : args.topicId,
                        onMessageReceived : function(message) {
                            var now = new Date().getTime();
                            var then = message.body - 0;
                            var delta = now - then;
                            cummulativeTime += delta;
                            if (delta > max)
                                max = delta;
                            if (delta < min || min == -1)
                                min = delta;
                            received++;
                            //addNotification(delta); 
                            if (received == messagesToSend) {
                                var average = cummulativeTime / received;
                                addNotification("Average: " + average + " ms");
                                addNotification("Max: " + max + " ms");
                                addNotification("Min: " + min + " ms");
                                $.pubsub.unsubscribe(args.topicId);
                                addNotification("----- end latencyTest -----");
                            }
                        },
                        onError : function(args) {
                            addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                            _latencyTestError = true;
                            addNotification("----- end latencyTest -----");
                        }
                    });

                    _latencyTestSent = messagesToSend;
                    _latencyTestTopicId = args.topicId;
                    _latencyTestInterval = window.setInterval("_latencyTestPublishOne()", messageDelay);
                },
                onError : function(args) {
                    addNotification("ERROR " + args.httpRequest.status + " " + args.httpRequest.statusText + ": " + args.httpRequest.responseText);
                    addNotification("----- end latencyTest -----");
                }
            })
        }
    </script>

</head>
<body>
    <table style="width: 100%; font-family: Arial, Helvetica, sans-serif; font-size: 11px;" cellspacing="2">
        <tr>
            <td style="width: 300px; vertical-align: top">
                Topic ID: <input id="topicId" type="text" value="" style="width: 100%"/>
                Message ID: <input id="messageId" type="text" value="" style="width: 100%"/>
                Message number: <input id="messageNum" type="text" value="10" style="width: 100%"/>
                Message delay [ms]: <input id="messageDelay" type="text" value="1000" style="width: 100%"/>
                Message: <textarea id="message" rows="5" style="width: 100%"></textarea><br/><br/>
                <input id="createButton" type="button" value="Create topic" style="width: 100%" onclick="onCreateTopic();"/>
                <input id="subscribeButton" type="button" value="Subscribe to topic" style="width: 100%" onclick="onSubscribe();"/>
                <input id="unsubscribeButton" type="button" value="Unsubscribe from topic" style="width: 100%" onclick="onUnsubscribe();"/>
                <input id="unsubscribeAllButton" type="button" value="Unsubscribe all" style="width: 100%" onclick="onUnsubscribeAll();"/>
                <input id="publishButton" type="button" value="Publish message" style="width: 100%" onclick="onPublish();"/>
                <input id="latencyTestButton" type="button" value="Latency test" style="width: 100%" onclick="latencyTest();"/>
            </td>
            <td><textarea id="notifications" rows="30" readonly="readonly" style="width: 100%; background-color: ButtonFace"></textarea>
            </td>
        </tr>
    </table>
    <script language="javascript">
        document.getElementById("topicId").value = "";
        document.getElementById("messageId").value = "";
        document.getElementById("message").value = "";
        document.getElementById("notifications").value = "";
    </script>
</body>
</html>
